#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @File : base_page.py
# @Autor: FY
# @Date: 2021/10/12
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.remote.webdriver import  WebDriver

class BasePage():

    def __init__(self, driver: WebDriver = None):
        print("driver value {}", driver)
        self.driver = driver

    def createBrowser(self):
        self.driver = webdriver.Chrome()
        return self

    def attachBrowser(self):
        option = Options()
        option.add_experimental_option("debuggerAddress", "127.0.0.1:9579")
        self.driver = webdriver.Chrome(options=option)
        return self




    def find_element(self, locator, timeout=10):
        eles = WebDriverWait(self.driver, timeout).until(lambda x: self.driver.find_elements(*locator))

        if len(eles) >= 1:
            return eles[0]
        return None

    def find_and_scroll_element(self, locator, timeout=10):
        size = self.driver.get_window_size()
        curheight = 0
        eles = None
        while curheight < size["height"]:
            ele = self.find_element(locator, timeout)
            if ele:
                return ele
            curheight += 500
            self.driver.execute_script("window.scrollTo(0,{})", curheight)
            sleep(3)
        return None

    def find_and_click_btn(self, locator):
        ele = self.find_and_scroll_element(locator, 10)
        if ele:
            ele.click()
